<?php

require_once('../domain/Sportart.php');
/**
 * Description of SportartDAO
 *
 * @author riemerb
 */
class SportartDAO {

    /**
     * Liefert alle Sportarten nach DB id sortiert.
     * @global type $db
     * @return type
     */
    public static function getAllSportarten() {
        global $db;
        $query = 'select id as id, sportname as sportname, doing as doing, sportler as sportler from sportart order by id asc';
        try {
            $statement = $db->prepare($query);
            $statement->execute();
            $results = $statement->fetchAll();
            $statement->closeCursor();
            $sportarten = SportartDAO::genDBO($results);
            return $sportarten;
        } catch (PDOException $e) {
            $error_message = $e->getMessage();
            display_db_error($error_message);
        }   
    }
    
    public static function genDBO($results) {
        $sportarten = array();
        foreach ($results as $row) {
            $sportart = new Sportart(
                            $row['id'],
                            $row['sportname'], 
                            $row['doing'],
                            $row['sportler']);
            $sportarten[] = $sportart;
        }
        return $sportarten;
    }
}

?>
